package com.wxd.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wxd.bean.CarBaseInfo;
import com.wxd.bean.CarChassis;
import com.wxd.bean.CarDriver;
import com.wxd.bean.CarEngine;
import com.wxd.bean.CarImage;
import com.wxd.bean.CarVehicle;
import com.wxd.service.CarInfoService;
import com.wxd.service.impl.CarInfoServiceImpl;
import com.wxd.util.UUIDGenerator;

public class CarInfoAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	//跳转到修改车辆基本信息页面
	public String editCarBaseInfoPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		CarBaseInfo carBaseInfo = carInfoService.getCarBaseInfoBean(uid);
		request.setAttribute("carBaseInfo", carBaseInfo);
		request.setAttribute("uid", uid);
		return SUCCESS;
	}
	//跳转到修改驾驶员信息页面
	public String editCarDriverPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String id = request.getParameter("id");
		CarDriver carDriver = carInfoService.getCarDriverBeanList(uid,id);
		request.setAttribute("carDriver", carDriver);
		request.setAttribute("uid", uid);
		return SUCCESS;
	}
	//跳转到修改车辆基本参数页面
	public String editVehicleInfoPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		CarVehicle carVehicle = carInfoService.getCarVehicleBean(uid);
		request.setAttribute("carVehicle", carVehicle);
		request.setAttribute("uid", uid);
		return SUCCESS;
	}
	//跳转到修改发动机参数页面
	public String editEngineInfoPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		CarEngine carEngine = carInfoService.getCarEngineBean(uid);
		request.setAttribute("carEngine", carEngine);
		request.setAttribute("uid", uid);
		return SUCCESS;
	}
	//跳转到修改地盘参数页面
	public String editChassisInfoPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		CarChassis carChassis = carInfoService.getCarChassisBean(uid);
		request.setAttribute("carChassis", carChassis);
		request.setAttribute("uid", uid);
		return SUCCESS;
	}
	//修改车辆基本信息
	public String editCarBaseInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		this.setUid(uid);

		String carUnit = request.getParameter("carUnit");
		String fileNum = request.getParameter("fileNum");
		String artificialNum = request.getParameter("artificialNum");
		String licensePlate = request.getParameter("licensePlate");
		String prepareDate = request.getParameter("prepareDate");
		String vehicleModel = request.getParameter("vehicleModel");
		String vehicleType = request.getParameter("vehicleType");
		String roadTransportNum = request.getParameter("roadTransportNum");
		String drivingLicenseNum = request.getParameter("drivingLicenseNum");
		String vehicleTechnologyLevel = request
				.getParameter("vehicleTechnologyLevel");
		String fuelType = request.getParameter("fuelType");
		String carLevelType = request.getParameter("carLevelType");
		String useProperty = request.getParameter("useProperty");
		String acquisitionDate = request.getParameter("acquisitionDate");
		String commissioningDate = request.getParameter("commissioningDate");
		String engineNum = request.getParameter("engineNum");
		String turfNum = request.getParameter("turfNum");
		String carCompany = request.getParameter("carCompany");
		String legalPerson = request.getParameter("legalPerson");
		String legalPersonPhone = request.getParameter("legalPersonPhone");
		String carOwner = request.getParameter("carOwner");
		String carOwnerPhone = request.getParameter("carOwnerPhone");
		String address = request.getParameter("address");

		CarBaseInfo carBaseInfo = new CarBaseInfo(uid, carUnit, fileNum,
				artificialNum, licensePlate, prepareDate, vehicleModel,
				vehicleType, roadTransportNum, drivingLicenseNum,
				vehicleTechnologyLevel, fuelType, carLevelType, useProperty,
				acquisitionDate, commissioningDate, engineNum, turfNum,
				carCompany, legalPerson, legalPersonPhone, carOwner,
				carOwnerPhone, address);

		boolean editFlag = carInfoService.editCarBaseInfo(carBaseInfo);
		return SUCCESS;
	}
	
	public String delCarInfo() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String uids = request.getParameter("uids");
		
		String[] uid = uids.split("\\|");
		boolean flag = carInfoService.delCarInfo(uid);
		
		JSONObject result = new JSONObject();
		
		
		if(flag){
			result.put("result", "success");
		}else{
			result.put("result", "failure");
		}
		
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("UTF-8"));
		return SUCCESS;
	}
	
	public String showCarDetail(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		CarBaseInfo carBaseInfo = carInfoService.getCarBaseInfoBean(uid);
		CarChassis carChassis = carInfoService.getCarChassisBean(uid);
		List<CarDriver> carDrivers = carInfoService.getCarDriverBeanList(uid);
		CarEngine carEngine = carInfoService.getCarEngineBean(uid);
		CarVehicle carVehicle = carInfoService.getCarVehicleBean(uid);
		List<CarImage> carImages = carInfoService.getCarImages(uid);
		
		request.setAttribute("uid", uid);
		request.setAttribute("carBaseInfo", carBaseInfo);
		request.setAttribute("carChassis",carChassis );
		request.setAttribute("carDrivers", carDrivers);
		request.setAttribute("carEngine", carEngine);
		request.setAttribute("carVehicle", carVehicle);
		request.setAttribute("carImages", carImages);
		return SUCCESS;
	}
	
	
	public String getCarBaseInfo() throws Exception {
		JSONObject result = new JSONObject();
		int totalRecord = carInfoService.getCarRecourdNum();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		if (totalRecord != 0) {
			int startNum = 0;
			int limitNum = 50;
			if (start != null) {
				try {
					startNum = Integer.parseInt(start);
					if (startNum >= totalRecord) {
						startNum = 0;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			if (limit != null) {
				try {
					limitNum = Integer.parseInt(limit);
					if (limitNum == 0) {
						limitNum = 50;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			
			JSONArray carBaseInfo = carInfoService.getCarBaseInfo(startNum, limitNum);
			result.put("root", carBaseInfo);
		} else {
			result.put("root", new JSONArray());
		}
		result.put("totalcount", totalRecord);

		this.inputStream = new ByteArrayInputStream(result.toString().getBytes(
				"utf-8"));
		return SUCCESS;
	}

	// 添加车辆基本信息
	public String addCarBaseInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String carUnit = request.getParameter("carUnit");
		String fileNum = request.getParameter("fileNum");
		String artificialNum = request.getParameter("artificialNum");
		String licensePlate = request.getParameter("licensePlate");
		String prepareDate = request.getParameter("prepareDate");
		String vehicleModel = request.getParameter("vehicleModel");
		String vehicleType = request.getParameter("vehicleType");
		String roadTransportNum = request.getParameter("roadTransportNum");
		String drivingLicenseNum = request.getParameter("drivingLicenseNum");
		String vehicleTechnologyLevel = request
				.getParameter("vehicleTechnologyLevel");
		String fuelType = request.getParameter("fuelType");
		String carLevelType = request.getParameter("carLevelType");
		String useProperty = request.getParameter("useProperty");
		String acquisitionDate = request.getParameter("acquisitionDate");
		String commissioningDate = request.getParameter("commissioningDate");
		String engineNum = request.getParameter("engineNum");
		String turfNum = request.getParameter("turfNum");
		String carCompany = request.getParameter("carCompany");
		String legalPerson = request.getParameter("legalPerson");
		String legalPersonPhone = request.getParameter("legalPersonPhone");
		String carOwner = request.getParameter("carOwner");
		String carOwnerPhone = request.getParameter("carOwnerPhone");
		String address = request.getParameter("address");

		String uuid = UUIDGenerator.getUUID();

		CarBaseInfo carBaseInfo = new CarBaseInfo(uuid, carUnit, fileNum,
				artificialNum, licensePlate, prepareDate, vehicleModel,
				vehicleType, roadTransportNum, drivingLicenseNum,
				vehicleTechnologyLevel, fuelType, carLevelType, useProperty,
				acquisitionDate, commissioningDate, engineNum, turfNum,
				carCompany, legalPerson, legalPersonPhone, carOwner,
				carOwnerPhone, address);

		boolean saveFlag = carInfoService.saveCarBaseInfo(carBaseInfo);

		if (saveFlag) {
			request.setAttribute("uid", uuid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	// 添加车辆照片
	public String addCarImage() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String realPath = getSavePath();

		String uid = request.getParameter("uid");

		File[] images = getImage();
		List<CarImage> list = new ArrayList<CarImage>();
		FileOutputStream fos = null;
		FileInputStream fis = null;
		for (int i = 0; i < images.length; i++) {
			CarImage carImage = new CarImage();
			carImage.setUid(uid);
			File file = new File(realPath + File.separator + uid
					+ File.separator + getImageFileName()[i]);
			carImage.setRelativePath(this.savePath.substring(1)+"/"+uid+"/"+getImageFileName()[i]);
			carImage.setImagePath(file.getPath());
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			fos = new FileOutputStream(file);

			fis = new FileInputStream(images[i]);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			list.add(carImage);
		}
		if (fis != null) {
			fis.close();
		}
		if (fos != null) {
			fos.close();
		}
		boolean saveFlag = carInfoService.saveCarImages(list);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	// 补充车辆照片
	public String addCarImage02() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String realPath = getSavePath();

		String uid = request.getParameter("uid");

		File[] images = getImage();
		List<CarImage> list = new ArrayList<CarImage>();
		FileOutputStream fos = null;
		FileInputStream fis = null;
		for (int i = 0; i < images.length; i++) {
			CarImage carImage = new CarImage();
			carImage.setUid(uid);
			File file = new File(realPath + File.separator + uid
					+ File.separator + getImageFileName()[i]);
			carImage.setRelativePath(this.savePath.substring(1)+"/"+uid+"/"+getImageFileName()[i]);
			carImage.setImagePath(file.getPath());
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			fos = new FileOutputStream(file);

			fis = new FileInputStream(images[i]);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			list.add(carImage);
		}
		if (fis != null) {
			fis.close();
		}
		if (fos != null) {
			fos.close();
		}
		boolean saveFlag = carInfoService.saveCarImages(list);
		JSONObject object = new JSONObject();
		
		if(saveFlag){
			object.put("success", new Boolean(true));
			object.put("msg", "上传成功");
		}else{
			object.put("success", new Boolean(false));
			object.put("msg", "上传失败");
		}
		
		this.inputStream = new ByteArrayInputStream(object.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	public String delCarImage() throws UnsupportedEncodingException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String id = request.getParameter("id");
		
		
		boolean flag = carInfoService.delCarImage(uid, id);
		
		JSONObject result = new JSONObject();
		if(flag){
			result.put("success", true);
			result.put("msg", "删除图片成功");
		}else{
			result.put("success", false);
			result.put("msg", "删除图片失败");
		}
		
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		
		return SUCCESS;
	}

	// 添加驾驶员信息
	public String addCarDriver() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();

		String uid = request.getParameter("uid");
		String driverName = request.getParameter("driverName");
		String idCard = request.getParameter("idCard");
		String address = request.getParameter("address");
		String telphone = request.getParameter("telphone");
		String drivingLicenceNum = request.getParameter("drivingLicenceNum");
		String drivingPermitNum = request.getParameter("drivingPermitNum");
		String drivingPermitType = request.getParameter("drivingPermitType");
		String drivingPermitDate = request.getParameter("drivingPermitDate");
		String quasiDriverYear = request.getParameter("quasiDriverYear");
		String quasiDriverYear02 = request.getParameter("quasiDriverYear02");

		String driverPhotoPath = "";
		String relativePath = "";
		String realPath = getSavePath();
		File[] images = getImage();
		if (images!=null&&images.length > 0) {
			File file = new File(realPath + File.separator + uid
					+ File.separator + getImageFileName()[0]);
			driverPhotoPath = file.getPath();
			relativePath = this.savePath.substring(1)+"/"+uid+"/"+getImageFileName()[0];
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);

			FileInputStream fis = new FileInputStream(images[0]);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

			fos.close();
			fis.close();
		}
		CarDriver cd = new CarDriver(null, uid, driverName, driverPhotoPath,
				idCard, address, telphone, drivingLicenceNum, drivingPermitNum,
				drivingPermitType, drivingPermitDate, quasiDriverYear,
				quasiDriverYear02);
		cd.setRelativePhotoPath(relativePath);
		
		boolean saveFlag = carInfoService.saveCarDriver(cd);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	
	public String addCarDriver02() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();

		String uid = request.getParameter("uid");
		String driverName = request.getParameter("driverName");
		String idCard = request.getParameter("idCard");
		String address = request.getParameter("address");
		String telphone = request.getParameter("telphone");
		String drivingLicenceNum = request.getParameter("drivingLicenceNum");
		String drivingPermitNum = request.getParameter("drivingPermitNum");
		String drivingPermitType = request.getParameter("drivingPermitType");
		String drivingPermitDate = request.getParameter("drivingPermitDate");
		String quasiDriverYear = request.getParameter("quasiDriverYear");
		String quasiDriverYear02 = request.getParameter("quasiDriverYear02");

		String driverPhotoPath = "";
		String relativePath = "";
		String realPath = getSavePath();
		File[] images = getImage();
		if (images!=null&&images.length > 0) {
			File file = new File(realPath + File.separator + uid
					+ File.separator + getImageFileName()[0]);
			driverPhotoPath = file.getPath();
			relativePath = this.savePath.substring(1)+"/"+uid+"/"+getImageFileName()[0];
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);

			FileInputStream fis = new FileInputStream(images[0]);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

			fos.close();
			fis.close();
		}
		CarDriver cd = new CarDriver(null, uid, driverName, driverPhotoPath,
				idCard, address, telphone, drivingLicenceNum, drivingPermitNum,
				drivingPermitType, drivingPermitDate, quasiDriverYear,
				quasiDriverYear02);
		cd.setRelativePhotoPath(relativePath);
		boolean saveFlag = carInfoService.saveCarDriver(cd);
		
		JSONObject result = new JSONObject();
		
		if(saveFlag){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	
	
	public String editCarDriverInfo() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String id = request.getParameter("id");
		String uid = request.getParameter("uid");
		String driverName = request.getParameter("driverName");
		String idCard = request.getParameter("idCard");
		String address = request.getParameter("address");
		String telphone = request.getParameter("telphone");
		String drivingLicenceNum = request.getParameter("drivingLicenceNum");
		String drivingPermitNum = request.getParameter("drivingPermitNum");
		String drivingPermitType = request.getParameter("drivingPermitType");
		String drivingPermitDate = request.getParameter("drivingPermitDate");
		String quasiDriverYear = request.getParameter("quasiDriverYear");
		String quasiDriverYear02 = request.getParameter("quasiDriverYear02");

		String driverPhotoPath = "";
		String relativePath = "";
		String realPath = getSavePath();
		File[] images = getImage();
		if (images!=null&&images.length > 0) {
			File file = new File(realPath + File.separator + uid
					+ File.separator + getImageFileName()[0]);
			driverPhotoPath = file.getPath();
			relativePath = this.savePath.substring(1)+"/"+uid+"/"+getImageFileName()[0];
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			FileOutputStream fos = new FileOutputStream(file);

			FileInputStream fis = new FileInputStream(images[0]);

			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

			fos.close();
			fis.close();
		}
		this.setUid(uid);
		CarDriver cd = new CarDriver(Integer.parseInt(id), uid, driverName, driverPhotoPath,
				idCard, address, telphone, drivingLicenceNum, drivingPermitNum,
				drivingPermitType, drivingPermitDate, quasiDriverYear,
				quasiDriverYear02);
		cd.setRelativePhotoPath(relativePath);
		
		boolean saveFlag = carInfoService.editCarDriverInfo(cd);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	
	public String delCarDriver() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String id = request.getParameter("id");
		boolean delFlag = carInfoService.delCarDriver(uid,id);
		
		JSONObject result = new JSONObject();
		if(delFlag){
			result.put("success", true);
			result.put("msg", "删除驾驶员信息成功");
		}else{
			result.put("success", false);
			result.put("msg", "删除驾驶员信息失败");
		}
		
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		return SUCCESS;
	}

	// 添加车辆技术概况及参数
	public String addVehicleInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String labelType = request.getParameter("labelType");
		String vehicleCategory = request.getParameter("vehicleCategory");
		String manufacturer = request.getParameter("manufacturer");
		String driveForm = request.getParameter("driveForm");
		String enginePosition = request.getParameter("enginePosition");
		String cabCar = request.getParameter("cabCar");
		String cockpitSeats = request.getParameter("cockpitSeats");
		String deadWeight = request.getParameter("deadWeight");
		String lightWeight = request.getParameter("lightWeight");
		String topSpeed = request.getParameter("topSpeed");
		String wheelbase = request.getParameter("wheelbase");
		String wheelTrack = request.getParameter("wheelTrack");
		String wheelTrack02 = request.getParameter("wheelTrack02");
		String overallDimensions = request.getParameter("overallDimensions");
		String minTurningDiameter = request.getParameter("minTurningDiameter");
		String brakingDistance = request.getParameter("brakingDistance");
		String fuelConsumption = request.getParameter("fuelConsumption");
		String minDistanceFromEarth = request
				.getParameter("minDistanceFromEarth");
		String maximumGradability = request.getParameter("maximumGradability");
		String fuelTankCapacity = request.getParameter("fuelTankCapacity");
		String remark = request.getParameter("remark");

		CarVehicle cv = new CarVehicle(null, uid, labelType, vehicleCategory,
				manufacturer, driveForm, enginePosition, cabCar, cockpitSeats,
				deadWeight, lightWeight, topSpeed, wheelbase, wheelTrack,
				wheelTrack02, overallDimensions, minTurningDiameter,
				brakingDistance, fuelConsumption, minDistanceFromEarth,
				maximumGradability, fuelTankCapacity, remark);

		boolean saveFlag = carInfoService.addVehicleInfo(cv);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	
	public String editVehicleInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String labelType = request.getParameter("labelType");
		String vehicleCategory = request.getParameter("vehicleCategory");
		String manufacturer = request.getParameter("manufacturer");
		String driveForm = request.getParameter("driveForm");
		String enginePosition = request.getParameter("enginePosition");
		String cabCar = request.getParameter("cabCar");
		String cockpitSeats = request.getParameter("cockpitSeats");
		String deadWeight = request.getParameter("deadWeight");
		String lightWeight = request.getParameter("lightWeight");
		String topSpeed = request.getParameter("topSpeed");
		String wheelbase = request.getParameter("wheelbase");
		String wheelTrack = request.getParameter("wheelTrack");
		String wheelTrack02 = request.getParameter("wheelTrack02");
		String overallDimensions = request.getParameter("overallDimensions");
		String minTurningDiameter = request.getParameter("minTurningDiameter");
		String brakingDistance = request.getParameter("brakingDistance");
		String fuelConsumption = request.getParameter("fuelConsumption");
		String minDistanceFromEarth = request
				.getParameter("minDistanceFromEarth");
		String maximumGradability = request.getParameter("maximumGradability");
		String fuelTankCapacity = request.getParameter("fuelTankCapacity");
		String remark = request.getParameter("remark");
		
		this.setUid(uid);
		CarVehicle cv = new CarVehicle(null, uid, labelType, vehicleCategory,
				manufacturer, driveForm, enginePosition, cabCar, cockpitSeats,
				deadWeight, lightWeight, topSpeed, wheelbase, wheelTrack,
				wheelTrack02, overallDimensions, minTurningDiameter,
				brakingDistance, fuelConsumption, minDistanceFromEarth,
				maximumGradability, fuelTankCapacity, remark);

		boolean saveFlag = carInfoService.editVehicleInfo(cv);
		return SUCCESS;
	}

	// 添加发动机技术参数
	public String addEngineInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String model = request.getParameter("model");
		String form = request.getParameter("form");
		String manufacturingPlant = request.getParameter("manufacturingPlant");
		String cylinderArrangement = request
				.getParameter("cylinderArrangement");
		String displacement = request.getParameter("displacement");
		String boreAndStroke = request.getParameter("boreAndStroke");
		String compressionRatio = request.getParameter("compressionRatio");
		String startupMode = request.getParameter("startupMode");
		String workOrder = request.getParameter("workOrder");
		String mostPowerful = request.getParameter("mostPowerful");
		String maximumTorque = request.getParameter("maximumTorque");
		String oilTypes = request.getParameter("oilTypes");
		String oilTypes02 = request.getParameter("oilTypes02");
		String remark = request.getParameter("remark");

		CarEngine ce = new CarEngine(null, uid, model, form,
				manufacturingPlant, cylinderArrangement, displacement,
				boreAndStroke, compressionRatio, startupMode, workOrder,
				mostPowerful, maximumTorque, oilTypes, oilTypes02, remark);
		boolean saveFlag = carInfoService.addEngineInfo(ce);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	public String editEngineInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String model = request.getParameter("model");
		String form = request.getParameter("form");
		String manufacturingPlant = request.getParameter("manufacturingPlant");
		String cylinderArrangement = request
				.getParameter("cylinderArrangement");
		String displacement = request.getParameter("displacement");
		String boreAndStroke = request.getParameter("boreAndStroke");
		String compressionRatio = request.getParameter("compressionRatio");
		String startupMode = request.getParameter("startupMode");
		String workOrder = request.getParameter("workOrder");
		String mostPowerful = request.getParameter("mostPowerful");
		String maximumTorque = request.getParameter("maximumTorque");
		String oilTypes = request.getParameter("oilTypes");
		String oilTypes02 = request.getParameter("oilTypes02");
		String remark = request.getParameter("remark");
		
		this.setUid(uid);
		
		CarEngine ce = new CarEngine(null, uid, model, form,
				manufacturingPlant, cylinderArrangement, displacement,
				boreAndStroke, compressionRatio, startupMode, workOrder,
				mostPowerful, maximumTorque, oilTypes, oilTypes02, remark);
		carInfoService.editEngineInfo(ce);
		return SUCCESS;
	}

	public String addChassisInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String clutch = request.getParameter("clutch");
		String transmission = request.getParameter("transmission");
		String shaft = request.getParameter("shaft");
		String mainTransmission = request.getParameter("mainTransmission");
		String reductionRatio = request.getParameter("reductionRatio");
		String bridgeShell = request.getParameter("bridgeShell");
		String halfShaft = request.getParameter("halfShaft");
		String differential = request.getParameter("differential");
		String frontSuspension = request.getParameter("frontSuspension");
		String afterSuspension = request.getParameter("afterSuspension");
		String tire = request.getParameter("tire");
		String tireSize = request.getParameter("tireSize");
		String wheelPressure = request.getParameter("wheelPressure");
		String wheelPressure02 = request.getParameter("wheelPressure02");
		String rim = request.getParameter("rim");
		String spareTire = request.getParameter("spareTire");
		String steeringGear = request.getParameter("steeringGear");
		String brakeFormBefore = request.getParameter("brakeFormBefore");
		String brakingAfter = request.getParameter("brakingAfter");
		String remark = request.getParameter("remark");

		CarChassis cc = new CarChassis(null, uid, clutch, transmission, shaft,
				mainTransmission, reductionRatio, bridgeShell, halfShaft,
				differential, frontSuspension, afterSuspension, tire, tireSize,
				wheelPressure, wheelPressure02, rim, spareTire, steeringGear,
				brakeFormBefore, brakingAfter, remark);
		boolean saveFlag = carInfoService.addChassisInfo(cc);
		if (saveFlag) {
			request.setAttribute("uid", uid);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}
	
	public String editChassisInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String uid = request.getParameter("uid");
		String clutch = request.getParameter("clutch");
		String transmission = request.getParameter("transmission");
		String shaft = request.getParameter("shaft");
		String mainTransmission = request.getParameter("mainTransmission");
		String reductionRatio = request.getParameter("reductionRatio");
		String bridgeShell = request.getParameter("bridgeShell");
		String halfShaft = request.getParameter("halfShaft");
		String differential = request.getParameter("differential");
		String frontSuspension = request.getParameter("frontSuspension");
		String afterSuspension = request.getParameter("afterSuspension");
		String tire = request.getParameter("tire");
		String tireSize = request.getParameter("tireSize");
		String wheelPressure = request.getParameter("wheelPressure");
		String wheelPressure02 = request.getParameter("wheelPressure02");
		String rim = request.getParameter("rim");
		String spareTire = request.getParameter("spareTire");
		String steeringGear = request.getParameter("steeringGear");
		String brakeFormBefore = request.getParameter("brakeFormBefore");
		String brakingAfter = request.getParameter("brakingAfter");
		String remark = request.getParameter("remark");
		
		this.setUid(uid);
		
		CarChassis cc = new CarChassis(null, uid, clutch, transmission, shaft,
				mainTransmission, reductionRatio, bridgeShell, halfShaft,
				differential, frontSuspension, afterSuspension, tire, tireSize,
				wheelPressure, wheelPressure02, rim, spareTire, steeringGear,
				brakeFormBefore, brakingAfter, remark);
		boolean saveFlag = carInfoService.editChassisInfo(cc);
		return  SUCCESS;
	}

	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public String[] getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String[] imageContentType) {
		this.imageContentType = imageContentType;
	}

	public String[] getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String[] imageFileName) {
		this.imageFileName = imageFileName;
	}

	public InputStream getResult() {
		return this.inputStream;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getUid() {
		return uid;
	}
	
	private String uid;
	private String savePath;
	private File[] image;
	private String[] imageContentType;
	private String[] imageFileName;
	private InputStream inputStream;
	private CarInfoService carInfoService = new CarInfoServiceImpl();
	
	
//-------------------------以下为combo中使用的下拉选项中查找数据的方法-------------------------
	public String getComboCarUnitList() throws Exception{
		JSONArray result = carInfoService.getComboCarUnitList();
		this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));
		return SUCCESS;
	}
	public String getComboVehicleTypeList() throws Exception{JSONArray result = carInfoService.getComboVehicleTypeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboVehicleTechnologyLevelList() throws Exception{JSONArray result = carInfoService.getComboVehicleTechnologyLevelList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboFuelTypeList() throws Exception{JSONArray result = carInfoService.getComboFuelTypeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboCarLevelTypeList() throws Exception{JSONArray result = carInfoService.getComboCarLevelTypeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboUsePropertyList() throws Exception{JSONArray result = carInfoService.getComboUsePropertyList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboCarCompanyList() throws Exception{JSONArray result = carInfoService.getComboCarCompanyList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	
	public String getComboDrivingPermitTypeList() throws Exception{JSONArray result = carInfoService.getComboDrivingPermitTypeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	
	public String getComboVehicleCategoryList() throws Exception{JSONArray result = carInfoService.getComboVehicleCategoryList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboDriveFormList() throws Exception{JSONArray result = carInfoService.getComboDriveFormList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboEnginePositionList() throws Exception{JSONArray result = carInfoService.getComboEnginePositionList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboCabCarList() throws Exception{JSONArray result = carInfoService.getComboCabCarList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	
	public String getComboFormList() throws Exception{JSONArray result = carInfoService.getComboFormList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboCylinderArrangementList() throws Exception{JSONArray result = carInfoService.getComboCylinderArrangementList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboBoreAndStrokeList() throws Exception{JSONArray result = carInfoService.getComboBoreAndStrokeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboCompressionRatioList() throws Exception{JSONArray result = carInfoService.getComboCompressionRatioList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboStartupModeList() throws Exception{JSONArray result = carInfoService.getComboStartupModeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboOilTypesList() throws Exception{JSONArray result = carInfoService.getComboOilTypesList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboOilTypes02List() throws Exception{JSONArray result = carInfoService.getComboOilTypes02List();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	
	public String getComboClutchList() throws Exception{JSONArray result = carInfoService.getComboClutchList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboTransmissionList() throws Exception{JSONArray result = carInfoService.getComboTransmissionList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboShaftList() throws Exception{JSONArray result = carInfoService.getComboShaftList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboReductionRatioList() throws Exception{JSONArray result = carInfoService.getComboReductionRatioList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboFrontSuspensionList() throws Exception{JSONArray result = carInfoService.getComboFrontSuspensionList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboAfterSuspensionList() throws Exception{JSONArray result = carInfoService.getComboAfterSuspensionList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboTireSizeList() throws Exception{JSONArray result = carInfoService.getComboTireSizeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboSpareTireList() throws Exception{JSONArray result = carInfoService.getComboSpareTireList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboSteeringGearList() throws Exception{JSONArray result = carInfoService.getComboSteeringGearList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboBrakeFormBeforeList() throws Exception{JSONArray result = carInfoService.getComboBrakeFormBeforeList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
	public String getComboBrakingAfterList() throws Exception{JSONArray result = carInfoService.getComboBrakingAfterList();this.inputStream = new ByteArrayInputStream(result.toString().getBytes("utf-8"));return SUCCESS;}
}
